<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>待办任务列表</title>
        <script src="https://unpkg.com/vue@3.5.13"></script>
    </head>
    <body>
        <div id="Application">
            <form v-on:submit.prevent="addTodo">
                <span>新建任务</span>
                <input type="text" v-model="newTodo" placeholder="请输入任务"/>
                <button type="submit">添加</button>
            </form>
            <ol>
                <li v-for="(item, index) in todos" :key="index">
                    {{ item }}
                    <button @click="removeTodo(index)">删除任务</button>
                </li>
            </ol>
        </div>

        <script>
            const App = {
                data() {
                    return {
                        // 待办任务列表
                        todos: [],
                        // 新建任务
                        newTodo: ""
                    }
                },
                methods: {
                    addTodo() {
                        // 判断输入内容是否为空
                        if (this.newTodo.length === 0) {
                            alert("请输入待办任务")
                            return
                        }
                        this.todos.push(this.newTodo)
                        this.newTodo = ""
                    },
                    removeTodo(index) {
                        if (this.todos.length === 0) {
                            return
                        }
                        this.todos.splice(index, 1)
                    }
                }
            }
            Vue.createApp(App).mount('#Application')
        </script>

    </body>
</html>